package com.wws.family.adapter

import android.app.Activity
import android.view.LayoutInflater
import android.view.ViewGroup
import com.wika.basics.base.BaseBindViewHolder
import com.wika.basics.base.BaseRecyclerViewAdapter
import com.wws.family.R
import com.wws.family.databinding.ItemAccountBinding
import com.wws.family.db.AccountBean
import com.wws.family.utils.DateUtils

/**
 *@Description:账本
 *@Date: 2025/1/7 22:28
 *@Author: WangWeiShuo
 */
class AccountAdapter(val context: Activity):BaseRecyclerViewAdapter<AccountBean,ItemAccountBinding>() {

    private var deleteCallBack: OnDeleteCallBack? = null

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ItemAccountBinding>,
        item: AccountBean?,
        position: Int
    ) {
        holder.binding.tvAccountName.text = item?.name
        val time = item?.time
        val week = DateUtils.obtainWeek(time,DateUtils.YYYYMMDD)
        holder.binding.tvAccountTime.text = time + "(${week})"

        val type = item?.type
        if(type == 0) {
            holder.binding.tvAccountPrice.text = "-${item?.price}元"
            when (item?.expType) {
                "Y" -> {
                    holder.binding.ivAccountIcon.background =
                        context.getDrawable(R.drawable.icon_yi)
                }

                "S" -> {
                    holder.binding.ivAccountIcon.background =
                        context.getDrawable(R.drawable.icon_shi)
                }

                "Z" -> {
                    holder.binding.ivAccountIcon.background =
                        context.getDrawable(R.drawable.icon_zhu)
                }

                "X" -> {
                    holder.binding.ivAccountIcon.background =
                        context.getDrawable(R.drawable.icon_xing_1)
                }

                else -> {
                    holder.binding.ivAccountIcon.background =
                        context.getDrawable(R.drawable.icon_account_default)
                }
            }
        }else{
            holder.binding.tvAccountPrice.text = "+${item?.price}元"
            holder.binding.ivAccountIcon.background = context.getDrawable(R.drawable.icon_income)
        }

        holder.binding.itemDelete.setOnClickListener {
            if(deleteCallBack != null){
                deleteCallBack?.onDelete(position,item)
            }
        }

    }

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ItemAccountBinding {
        return ItemAccountBinding.inflate(layoutInflater,parent,false)
    }

    fun addDeleteCallBack(callBack: OnDeleteCallBack){
        deleteCallBack = callBack
    }

    interface OnDeleteCallBack{
        fun onDelete(pos: Int,itemBean: AccountBean?)
    }
}

